var api = require('../../untils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */ 
  data: {
    status:false,
   items:[

   ],

    list: [
      {
        id: 1,
        name: "油烟机清洗",
        price: "89.00",
        term: "4小时/次/人，含檫玻璃和保洁。为了保障服务人员的生命安全，其中高层玻璃住。。。",
        number: "1",
      },
      {
        id: 1,
        name: "油烟机清洗",
        price: "89.00",
        term: "4小时/次/人，含檫玻璃和保洁。为了保障服务人员的生命安全，其中高层玻璃住。。。",
        number: "1",
      },
    ],
    total: [
      {
        total: "178.00",
      },
    ],
    userStars: [
        '/images/wujiao.png',
        '/images/wujiao.png',
        '/images/wujiao.png',
        '/images/wujiao.png',
        '/images/wujiao.png',
    ],
    pageBackgroundColor: ' rgba(237, 237, 237, 0.5)',
    comment_list: [],
    indexs:0,
    level:5
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var id = options.id;
    that.setData({
      id: id
    })
    var zong_mo = 0;
    app.Get(api.wxapp.OrderDetail, {
      id: id,
    }, function (res) {
      if (res.Code == 200) {
        var order_one = res.Data.CommodityDetailOne;
        for (var i = 0; i < order_one.length; i++) {
          zong_mo += (order_one[i]['Price'] * order_one[i]['Count'])
        };
        that.setData({
          order_number: res.Data.Number,
          order_address: res.Data.UserAddressHelp,
          CommoditySeriesName: res.Data.CommoditySeriesName,
          StateName: res.Data.StateName,
          order_data: res.Data,
          zong_mo: zong_mo
        })
      }
      else {
        wx.showToast({
          title: res.Msg,
          icon: 'none',
          duration: 2000,
        });
      }
    })
  },
    submit: function() {
      this.setData({
        status: true
      })
    },
    close_shadow: function() {
      this.setData({
        status: false
      }) 
  },
  // 星星点击事件
  starTap: function (e) {
    var that =  this;
    var index = e.currentTarget.dataset.index; // 获取当前点击的是第几颗星星
    var tempUserStars = this.data.userStars; // 暂存星星数组
    var len = tempUserStars.length; // 获取星星数组的长度
    console.log(index + "index");
    var stars = [];
    for (var i = 0; i <= len-1; i++) {
      if (i <= index) { // 小于等于index的是满心
        stars[i] = '/images/wujiao.png'
      } else { // 其他是空心
        stars[i] = '/images/heixing.png'
      }
    }
    var level = index+1;
    that.get_template(level)
    // 重新赋值就可以显示了
    this.setData({
      userStars: stars,
      level: level
    })
  },
  selected:function(e){
    var that = this;
    var len = that.data.comment_list.length;
    var arr = that.data.comment_list;
    var index = e.currentTarget.dataset.index;
    var state = e.currentTarget.dataset.state;
    for (var i = 0; i <= len - 1; i++) {
       if(index == i){
         if (state == 0){
           arr[i].state = 1;
          }else{
           arr[i].state = 0;
          }
       }
    }
    console.log(arr);
    that.setData({
      comment_list: arr
    })
  },
    change: function() {
      var bgColor = this.data.pageBackgroundColor == '#3386FF' ? 'rgba(237, 237, 237, 0.5)' : '#3386FF';
      this.setData({
        pageBackgroundColor: bgColor
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:function(){
    var that = this;
    var level = that.data.level;
    that.get_template(level)
  },
 
  //提交表单
  fromSubmit: function (e) {
    var that = this;
    var id = that.data.id;
    var Level = that.data.level;
    var Content = e.detail.value.Content;
    var OrderId = that.data.id;
    var title = '';
    var comment_list = that.data.comment_list;
    for (var i = 0; i<=comment_list.length-1;i++){
      if (comment_list[i]['state'] == 1){
        title += comment_list[i]['Title'] +',';
      }
    }
    if(title != ''){
      title = title.substring(0, title.length - 1);
    }
    app.Post(api.wxapp.PostEvaluate, {
      Level: Level,
      Title: title,
      Content: Content,
      OrderId: id,
    }, function (res) {
      var msg = res.Msg
      if (res.Code == 200) {
        var toke = res.Data.Token
        wx.showToast({
          title: msg,
          icon: 'none',
          duration: 2000,
          success:function(){
            setTimeout(function(){
              wx.navigateBack({
                delta: 1
              })
            },2000)
          }
        });
      } else {
        wx.showToast({
          title: msg,
          icon: 'none',
          duration: 2000,
        });
      }

    })

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  get_template(level){
    var that = this;
    app.Get(api.wxapp.GetEvaluatemplate, {
      level: level,
    }, function (res) {
      var comment_list = res.Data;
      var datas = [];
      for (var i = 0; i < comment_list.length; i++) {
        var obj = {};
        obj.index = i;
        obj.Title = comment_list[i];
        obj.state = 0;
        datas.push(obj)
      }
      if (res.Code == 200) {
        level: that.data.level;
        that.setData({
          comment_list: datas
        })

      }
      else {
        var msg = res.Msg;
        wx.showToast({
          title: msg,
          icon: 'none',
          duration: 2000,
        });
      }
    })
  }
})